﻿using ArtMath.CommonType;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ArtMath.Command
{
    public abstract class CommandBase : ICommand
    {
        ToolStripMenuItem undoItem, redoItem;
        public abstract int ObjectCount { get; }
        public ToolStripMenuItem RedoMenuItem
        {
            get { return redoItem; }
            protected set
            {
                redoItem = value;
                if (redoItem != null)
                {
                    redoItem.Tag = this;
                }
            }
        }
        public ToolStripMenuItem UndoMenuItem
        {
            get { return undoItem; }
            protected set
            {
                undoItem = value;
                if (undoItem != null)
                {
                    undoItem.Tag = this;
                }
            }
        }

        public abstract void Redo();
        public abstract void Undo();
        public static string ZipName(IEnumerable objs, int max = 3)
        {
            List<string> names = new List<string>();
            foreach (object item in objs)
            {
                names.Add(item.ToString());
            }
            return ZipName(names, max);
        }
        public static string ZipName<T>(IEnumerable<T> objs, int max = 3) where T : IName
        {
            List<string> names = new List<string>();
            foreach (T item in objs)
            {
                names.Add(item.Name);
            }
            return ZipName(names, max);
        }
        public static string ZipName(IEnumerable<string> names, int max = 3)
        {
            StringBuilder sb = new StringBuilder();
            int count = 0;
            foreach (string item in names)
            {
                count++;
                if (count <= max)
                {
                    sb.Append($"\"{item}\", ");
                }
                else
                {
                    break;
                }
            }
            if (count == 0 || sb.Length == 0) return string.Empty;
            sb.Remove(sb.Length - 2, 2);
            if (count > max)
            {
                sb.Append("...");
            }
            return sb.ToString();
        }
    }
}
